<?php
/*----------------------------------------------------------------------
 * 小影院网吧版本业务
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Common\Logic;

class InternetBarLogic
{
	
	/**
	 * pc使用信息更新
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function syncPcUseInfoUpdate($params)
	{
        $result = array();
        $result_manager = array(
            0 => array('status_code'=>0, 'status_message'=>'数据更新失败'),
            1 => array('status_code'=>1, 'status_message'=>'数据更新成功'),
            3 => array('status_code'=>3, 'status_message'=>'参数错误'),
        );
        $status_code = 0;
        if(isset($params['host_name']) && !empty($params['host_name'])
		   && isset($params['data_list']) && !empty($params['data_list'])) {
			$model = M('NetbarUseInfo');
			$cinemaLogic = new \Common\Logic\CinemaLogic();
            $current_time = time();
			$update_ids = array();
			$status_code = 1;
			
			//查询影院相关信息
			$cinema = $cinemaLogic->findByAttr(array('host_name'=>$params['host_name']));
			
            foreach($params['data_list'] as $key => $value) {
                $condition = array();
                $condition['host_name'] = $params['host_name'];
                $condition['pc_number'] = $value['pc_number'];
				$condition['start_time'] = $value['start_time'];
                $check_rs = $model->where($condition)->find();
                if(!$check_rs) {
					$value['is_update'] = 1;
					$value['cinema_id'] = $value['id'];
					$value['host_name'] = $params['host_name'];
					$value['agent_id'] = $cinema['agent_id'];
					$value['agent_level_1'] = $cinema['agent_level_1'];
					$value['agent_level_2'] = $cinema['agent_level_2'];
					$value['upload_time'] = $current_time;
					unset($value['id']);
                    $db_result = $model->data($value)->add();
					if($db_result) {
						$update_ids[] = $value['cinema_id'];
					}
                }
            }
			
			if($update_ids) {
				$result['update_ids'] = $update_ids;
			}
			
        } else {
            $status_code = 3;
        }
        
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
	}
	
	/**
	 * pc播放信息更新
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function syncPcPlayInfoUpdate($params)
	{
        $result = array();
        $result_manager = array(
            0 => array('status_code'=>0, 'status_message'=>'数据更新失败'),
            1 => array('status_code'=>1, 'status_message'=>'数据更新成功'),
            3 => array('status_code'=>3, 'status_message'=>'参数错误'),
        );
        $status_code = 0;
        if(isset($params['host_name']) && !empty($params['host_name'])
		   && isset($params['data_list']) && !empty($params['data_list'])) {
			$model = M('NetbarPlayInfo');
            $current_time = time();
			$update_ids = array();
			$update_video_ids = array();
			$status_code = 1;
			
            foreach($params['data_list'] as $key => $value) {
                $condition = array();
                $condition['host_name'] = $params['host_name'];
                $condition['pc_number'] = $value['pc_number'];
				$condition['start_time'] = $value['start_time'];
				$condition['video_id'] = $value['video_id'];
                $check_rs = $model->where($condition)->find();
                if(!$check_rs) {
					$value['is_update'] = 1;
					$value['cinema_id'] = $value['id'];
					$value['host_name'] = $params['host_name'];
					$value['upload_time'] = $current_time;
					unset($value['id']);
                    $db_result = $model->data($value)->add();
					if($db_result) {
						$update_ids[] = $value['cinema_id'];
						//播放信息更新
						$temp_update_video_info = array();
						$temp_update_video_info['is_cloud_video'] = $value['is_cloud_video'];
						$temp_update_video_info['video_id'] = $value['video_id'];
						$update_video_ids[] = $temp_update_video_info;
					}
                }
            }
			
			if($update_ids) {
				$result['update_ids'] = $update_ids;
			}
			
			if($update_video_ids) {
				$result['update_video_ids'] = $update_video_ids;
			}
			
        } else {
            $status_code = 3;
        }
        
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
	}
	
}
